//jqGrid的配置信息
$.jgrid.defaults.width = 1000;
$.jgrid.defaults.responsive = true;
$.jgrid.defaults.styleUI = 'Bootstrap';

//工具集合Tools
window.T = {};

// 获取请求参数
// 使用示例
// location.href = http://localhost:8080/index.html?id=123
// T.p('id') --> 123;
var url = function(name) {
	var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
	var r = window.location.search.substr(1).match(reg);
	if(r!=null)return  unescape(r[2]); return null;
};
T.p = url;

//全局配置
$.ajaxSetup({
	dataType: "json",
	contentType: "application/json",
	cache: false
});
$(document).ajaxError(function(event, jqxhr, settings, exception) {
    vm.errorMsg = jqxhr.responseJSON.message
});
$(document).ajaxSuccess(function() {
    vm.errorMsg =""
});

//重写alert
window.alert = function(msg, callback){
	parent.layer.alert(msg, function(index){
		parent.layer.close(index);
		if(typeof(callback) === "function"){
			callback("ok");
		}
	});
}

//重写confirm式样框
window.confirm = function(msg, callback){
	parent.layer.confirm(msg, {btn: ['确定','取消']},
	function(){//确定事件
		if(typeof(callback) === "function"){
			callback("ok");
		}
	});
}

//选择一条记录
function getSelectedRow(gridId) {
	if(gridId==null || typeof gridId=='undefined'){
		gridId = "#jqGrid";
	}
    var grid = $(gridId);
    var rowKey = grid.jqGrid('getGridParam',"selrow");
    if(!rowKey){
    	alert("请选择一条记录");
    	return ;
    }

    var selectedIDs = grid.getGridParam("selarrrow");
    if(selectedIDs.length > 1){
    	alert("只能选择一条记录");
    	return ;
    }
    
    return selectedIDs[0];
}

//选择多条记录
function getSelectedRows() {
    var grid = $("#jqGrid");
    var rowKey = grid.getGridParam("selrow");
    if(!rowKey){
    	alert("请选择一条记录");
    	return ;
    }
    
    return grid.getGridParam("selarrrow");
}

/**
 * 从JSON对象生成JSON Array，用于From的POST提交
 * 示例
 * @param model
 * @param prefix
 */
function getJSONArrayFromObject(model,prefix){
	if(prefix==null) prefix = "t";
    var json = new Array();
    for(var key in model){
    	if(model[key] instanceof  Array){
			var ids = model[key]
			ids.forEach(function(item,index){
				if(item instanceof  Object)
					json.push({name:prefix+"."+key+".id", value: item.id})
				else
                    json.push({name:prefix+"."+key+".id", value: item})
            })
		}else  if(model[key] instanceof  Object){
            var obj = model[key]
            json.push({name:prefix+"."+key+".id",value: obj.id });
        }else{
            json.push({name:prefix+"."+key,value:model[key]});
        }
    }
    return json;
}

function getJSONObjectFromObject(model,prefix){
    if(prefix==null) prefix = "t";
    var json ={};
    for(var key in model){
    	if(model[key] instanceof  Array){
    		var arr = model[key]
			for (var x in arr){
    			json[prefix+"."+key] = arr[x]
			}
		}else if(model[key] instanceof  Object){
			var obj = model[key]
			for(var prop in obj){
				json[prefix+"."+key+"."+prop] = obj[prop]
			}
        }else {
            json[prefix+"."+key] = model[key];
		}
    }
    return json;
}


/**
 * JqGrid
 **/
var custom = {

	// grid 通用设定
    gridCommonSetting : function (colModel) {
		return {
            datatype: "json",
            colModel: colModel,
            viewrecords: true,
            height: 'auto',
            rowNum: 10,
            rowList: [10, 30, 50],
            rownumbers: true,
            rownumWidth: 25,
            autowidth: true,
            shrinkToFit:true,
            multiselect: true,
            jsonReader: {
                root: "page.list",
                page: "page.currPage",
                total: "page.totalPage",
                records: "page.totalCount"
            },
            prmNames: {
                page: "page",
                rows: "limit",
                order: "order"
            },
            gridComplete:function(){
                //隐藏grid底部滚动条
                $(this).closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
            }
        }
    }
}